clear all
set more off
set mem 10000000
set matsize 10000
version 13

****************************************************************** 
*** Build File to Process Raw Census 2011 Village Directory ******
****************************************************************** 

** Set file paths
do "$path_code/paths.do"

********************************************************************************
********************************************************************************

* Insheet xlsx's and rename variables
{
cd "$vd11/raw village xlsx/"
local allfiles : dir . files "*.xlsx" 
foreach f of local allfiles {

import excel "`f'", case(lower) allstring clear
  
	// Rename variables
{
rename A st_code11
rename B state11
rename C dt_code11
rename D district11
rename E bk_code1_vd11
rename F block1_vd11
rename G vi_code11
rename H village_vd11
rename I bk_code2_vd11
rename J block2_vd11
rename K panch_code11
rename L panchayat_11
rename M ref_year
rename N subdist_hq
rename O suddist_hq_dist
rename P dist_hq
rename Q dist_hq_dist
rename R near_st_town
rename S near_st_town_dist
drop T
drop U
drop V
drop W
rename X area
rename Y t_hh
rename Z t_p
rename AA t_m
rename AB t_f
rename AC sc_p
rename AD sc_m
rename AE sc_f
rename AF st_p
rename AG st_m
rename AH st_f
drop AI
rename AJ pp_sch_g
drop AK
rename AL pp_sch_p
rename AM pp_sch_near_gp
rename AN pp_sch_near_town
rename AO rang_pp_sch
drop AP
rename AQ p_sch_g
drop AR
rename AS p_sch_p
rename AT p_sch_near_gp
rename AU p_sch_near_town
rename AV rang_p_sch
drop AW
rename AX m_sch_g
drop AY
rename AZ m_sch_p
rename BA m_sch_near_gp
rename BB m_sch_near_town
rename BC rang_m_sch
drop BD
rename BE s_sch_g
drop BF
rename BG s_sch_p
rename BH s_sch_near_gp
rename BI s_sch_near_town
rename BJ rang_s_sch
drop BK
rename BL s_s_sch_g
drop BM
rename BN s_s_sch_p
rename BO s_s_sch_near_gp
rename BP s_s_sch_near_town
rename BQ rang_s_s_sch
drop BR
rename BS college_as_g
drop BT
rename BU college_as_p
rename BV college_as_near_gp
rename BW college_as_near_town
rename BX rang_college_as
drop BY
rename BZ college_en_g
drop CA
rename CB college_en_p
rename CC college_en_near_gp
rename CD college_en_near_town
rename CE rang_college_en
drop CF
rename CG college_med_g
drop CH
rename CI college_med_p
rename CJ college_med_near_gp
rename CK college_med_near_town
rename CL rang_college_med
drop CM
rename CN college_mng_g
drop CO
rename CP college_mng_p
rename CQ college_mng_near_gp
rename CR college_mng_near_town
rename CS rang_college_mng
drop CT
rename CU college_pol_g
drop CV
rename CW college_pol_p
rename CX college_pol_near_gp
rename CY college_pol_near_town
rename CZ rang_college_pol
drop DA
rename DB tr_sch_voc_g
drop DC
rename DD tr_sch_voc_p
rename DE tr_sch_voc_near_gp
rename DF tr_sch_voc_near_town
rename DG rang_tr_sch_voc
drop DH
rename DI tr_sch_nf_g
drop DJ
rename DK tr_sch_nf_p
rename DL tr_sch_nf_near_gp
rename DM tr_sch_nf_near_town
rename DN rang_tr_sch_nf
drop DO
rename DP dis_sch_g
drop DQ
rename DR dis_sch_p
rename DS dis_sch_near_gp
rename DT dis_sch_near_town
rename DU rang_dis_sch
drop DV
rename DW oth_sch_g
drop DX
rename DY oth_sch_p
rename DZ oth_sch_near_gp
rename EA oth_sch_near_town
rename EB rang_oth_sch
rename EC ch_cntr
rename ED ch_cntr_dr_ts
rename EE ch_cntr_dr_ip
rename EF ch_cntr_ps_ts
rename EG ch_cntr_ps_ip
rename EH rang_chc
rename EI ph_cntr
rename EJ ph_cntr_dr_ts
rename EK ph_cntr_dr_ip
rename EL ph_cntr_ps_ts
rename EM ph_cntr_ps_ip
rename EN rang_phc
rename EO phs_cnt
rename EP phs_cnt_dr_ts
rename EQ phs_cnt_dr_ip
rename ER phs_cnt_ps_ts
rename ES phs_cnt_ps_ip
rename ET rang_phsc
rename EU mcw_cntr
rename EV mcw_cntr_dr_ts
rename EW mcw_cntr_dr_ip
rename EX mcw_cntr_ps_ts
rename EY mcw_cntr_ps_ip
rename EZ rang_mcw
rename FA tb_cln
rename FB tb_cln_dr_ts
rename FC tb_cln_dr_ip
rename FD tb_cln_ps_ts
rename FE tb_cln_ps_ip
rename FF rang_tb_cln
rename FG all_hosp
rename FH all_hosp_dr_ts
rename FI all_hosp_dr_ip
rename FJ all_hosp_ps_ts
rename FK all_hosp_ps_ip
rename FL rang_all
rename FM alt_hosp
rename FN alt_hosp_dr_ts
rename FO alt_hosp_dr_ip
rename FP alt_hosp_ps_ts
rename FQ alt_hosp_ps_ip
rename FR rang_alt
rename FS disp
rename FT disp_dr_ts
rename FU disp_dr_ip
rename FV disp_ps_ts
rename FW disp_ps_ip
rename FX rang_disp
rename FY vet_hosp
rename FZ vet_hosp_dr_ts
rename GA vet_hosp_dr_ip
rename GB vet_hosp_ps_ts
rename GC vet_hosp_ps_ip
rename GD rang_vet
rename GE mh_clin
rename GF mh_clin_dr_ts
rename GG mh_clin_dr_ip
rename GH mh_clin_ps_ts
rename GI mh_clin_ps_ip
rename GJ rang_mhc
rename GK fw_cntr
rename GL fw_cntr_dr_ts
rename GM fw_cntr_dr_ip
rename GN fw_cntr_ps_ts
rename GO fw_cntr_ps_ip
rename GP rang_fwc
rename GQ ng_med_op
rename GR ng_med_iop
rename GS ng_med_char
rename GT ng_med_deg_mbbs
rename GU ng_med_deg_oth
rename GV ng_med_deg_none
rename GW ng_med_tp_fh
rename GX ng_med_ms
rename GY ng_med_oth
rename GZ tap_tr
rename HA tap_tr_ay
rename HB tap_tr_sum
rename HC tap_untr
rename HD tap_untr_ay
rename HE tap_untr_sum
rename HF well_cov
rename HG well_cov_ay
rename HH well_cov_sum
rename HI well_uncov
rename HJ well_uncov_ay
rename HK well_uncov_sum
rename HL handpump
rename HM handpump_ay
rename HN handpump_sum
rename HO tubewell
rename HP tubewell_ay
rename HQ tubewell_sum
rename HR spring
rename HS spring_ay
rename HT spring_sum
rename HU river_canal
rename HV river_canal_ay
rename HW river_canal_sum
rename HX tank_lake
rename HY tank_lake_ay
rename HZ tank_lake_sum
rename IA other
rename IB other_ay
rename IC other_sum
rename ID drain_cl
rename IE drain_op
rename IF drain_none
rename IG drain_p_cov
rename IH drain_p_uncov
rename II drain_k
rename IJ drain_disch_wb
rename IK cov_tsc
rename IL com_toilet_bath
rename IM com_toilet_nobath
rename IN rpc_sho
rename IO rpm_sho
rename IP com_waste_disp
rename IQ com_bg_recycle
rename IR com_no_waste_disp
rename IS post_off_fac
rename IT rang_po
rename IU post_off_sub_fac
rename IV rang_pos
rename IW post_tele_fac
rename IX rang_post_tele
rename IY vill_pin_fac
rename IZ rang_vill_pin
rename JA vill_pin
rename JB phone_ll
rename JC rang_phone_ll
rename JD pco_mobile_fac
rename JE rang_pco_mobile
rename JF phone_mob_fac
rename JG rang_phone_mob
rename JH int_cafe_fac
rename JI rang_int_cafe
rename JJ courier_fac
rename JK rang_courier
rename JL bs_pri_fac
rename JM rang_bs_pri
rename JN bs_pub_fac
rename JO rang_bs_pub
rename JP rs_fac
rename JQ rang_rs
rename JR auto_fac
rename JS rang_auto
rename JT taxi_fac
rename JU rang_taxi
rename JV van_fac
rename JW rang_van
rename JX tractor_fac
rename JY rang_tractor
rename JZ rickshaw_man_fac
rename KA rang_rickshaw_man
rename KB rickshaw_mach_fac
rename KC rang_rickshaw_mach
rename KD carts_fac
rename KE rang_carts
rename KF ferry_fac
rename KG rang_ferry
rename KH ntl_hwy_fac
rename KI rang_ntl_hwy
rename KJ st_hwy_fac
rename KK rang_st_hwy
rename KL road_md_fac
rename KM rang_road_md
rename KN road_od_fac
rename KO rang_road_od
rename KP paved_road_fac
rename KQ rang_paved_road
rename KR grav_road_fac
rename KS rang_grav_road
rename KT wbm_fac
rename KU rang_wbm
rename KV aw_road_fac
rename KW rang_aw_road
rename KX navig_fac
rename KY rang_navig
rename KZ footpath_fac
rename LA rang_footpath
rename LB atm_fac
rename LC dist_atm_km
rename LD comm_bank_fac
rename LE rang_comm
rename LF coop_bank_fac
rename LG rang_coop
rename LH ac_soc_fac
rename LI rang_acs
rename LJ shg_fac
rename LK rang_shg
rename LL pub_dist_fac
rename LM rang_pub_dist
rename LN mandi_fac
rename LO rang_mandi
rename LP haat_fac
rename LQ rang_haat
rename LR am_soc_fac
rename LS rang_am_soc
rename LT nut_cntr_icds_fac
rename LU rang_nut_cntr_icds
rename LV nut_cntr_ang_fac
rename LW rang_nut_cntr_ang
rename LX nut_cntr_oth_fac
rename LY rang_nut_cntr_oth
rename LZ asha_fac
rename MA rang_asha
rename MB comm_cntr_fac
rename MC rang_comm_cntr
rename MD sp_field_fac
rename ME rang_sp_field
rename MF sp_cl_fac
rename MG rang_sp_cl
rename MH c_v_hall_fac
rename MI rang_cv
rename MJ library_fac
rename MK rang_library
rename ML read_room_fac
rename MM rang_read_room
rename MN news_pap_fac
rename MO rang_news_pap
rename MP assemb_ps_fac
rename MQ rang_assemb_ps
rename MR bdro_fac
rename MS rang_bdro
rename MT power_dom
rename MU power_dom_sum_hr
rename MV power_dom_win_hr
rename MW power_agr
rename MX power_agr_sum_hr
rename MY power_agr_win_hr
rename MZ power_com
rename NA power_com_sum_hr
rename NB power_com_win_hr
rename NC power_all
rename ND power_all_sum_hr
rename NE power_all_win_hr
rename NF agr_comm1
rename NG man_comm1
rename NH hand_comm1
rename NI agr_comm2
rename NJ man_comm2
rename NK hand_comm2
rename NL agr_comm3
rename NM man_comm3
rename NN hand_comm3
rename NO area_fores
rename NP area_non_ag
rename NQ area_na_cu
rename NR area_pasture
rename NS area_treecrop
rename NT area_cult_waste
rename NU area_fallow_oth
rename NV area_fallow_cur
rename NW area_sown
rename NX area_unirrigated
rename NY area_irrigated
rename NZ area_irr_canal
rename OA area_irr_w_twell
rename OB area_irr_tank_lake
rename OC area_irr_waterfall
rename OD area_irr_oth
rename OE near_town
rename OF dist_town
}

drop in 1
duplicates drop
di "`f'"
local newname = subinstr("`f'",".xlsx","",1)
compress
foreach v of varlist * {
local vartype: type `v'
  if real(substr("`vartype'",4,10))>244 { 
	  recast str244 `v', force
  }
}
save "$vd11/raw village dta files/`newname'.dta", replace
}
}

* Clean raw vd
{
clear
cd "$vd11/raw village dta files/"
local allfiles : dir . files "*.dta" 
foreach f of local allfiles {
	append using "`f'"
}

drop OG
destring *, replace
drop if st_code==.
duplicates r st_code11 dt_code11 vi_code11

foreach v of varlist * {
  cap replace `v' = trim(itrim(upper(`v')))
	cap replace `v' = "" if `v'=="N.A."
  cap replace `v' = "" if `v'=="NA"
	cap replace `v' = "" if `v'=="-"
	cap replace `v' = "" if `v'=="."
}

replace bk_code2_vd11 = word(subinstr(bk_code2_vd11,","," ",.),1)
destring bk_code2_vd11, replace

compress
save "$vd11/vd_2011_all_raw.dta", replace
}

* Process raw vd
{
use "$vd11/vd_2011_all_raw.dta", clear

foreach v of varlist rang* {
	replace `v' = "1" if `v'=="A"
	replace `v' = "2" if `v'=="B"
	replace `v' = "3" if `v'=="C"
}

destring *, replace
foreach v of varlist tap_tr-com_no_waste_disp power_dom power_agr power_com power_all phone_ll *_fac {
  destring `v', replace force
	replace `v' = . if `v'<1 | `v'>2
	replace `v' = 0 if `v'==2
	di "`v'"
	assert `v'==0 | `v'==1 | `v'==.
}

destring college_pol_near_gp mcw_cntr mcw_cntr_ps_ip rang_disp vet_hosp_ps_ip rang_pco_mobile ///
   rang_grav_road power_all_win_hr area_unirrigated area_irr_oth, replace force

duplicates drop st_code11 dt_code11 bk_code1_vd11 bk_code2_vd11 vi_code11, force
rename t_p t_p_vd
gen vd11_id = _n
order vd11_id

*Label variables
{
la var st_code11 "2011 state code"
la var state11 "State name"
la var dt_code11 "2011 district code"
la var district11 "District name"
la var bk_code1_vd11 "2011 block code (sub-district)"
la var block1_vd11 "Block name (sub-district)"
la var vi_code11 "2011 village code"
la var village_vd11 "Village name"
la var bk_code2_vd11 "2011 block code (CD block)"
la var block2_vd11 "Block name (CD block)"
la var panch_code11 "2011 panchayat code"
la var panchayat_11 "Panchayat name"
la var ref_year "Reference year"
la var subdist_hq "Sub-district headquarters"
la var suddist_hq_dist "Sub-district headquarters (distance in km)"
la var dist_hq "District headquarters "
la var dist_hq_dist "District headquarters (distances in km)"
la var near_st_town "Nearest statutory town"
la var near_st_town_dist "Nearest statutory town (distance in km)"
la var area "Village area (hectares)"
la var t_hh "Number of households"
la var t_p "Village population"
la var t_m "Village population, male"
la var t_f "Village population, female"
la var sc_p "Village population (SC)"
la var sc_m "Village population (SC), male"
la var sc_f "Village population (SC), female"
la var st_p "Village population (ST)"
la var st_m "Village population (ST), male"
la var st_f "Village population (ST), female"
la var pp_sch_g "Number of pre-primary schools, gov't (nursery/LKG/UKG)"
la var pp_sch_p "Number of pre-primary schools, private (nursery/LKG/UKG)"
la var pp_sch_near_gp "Nearest pre-primary school status (1 = gov't, 2 = private)"
la var pp_sch_near_town "Nearest pre-primary school town"
la var rang_pp_sch "Range to nearest pre-primary school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var p_sch_g "Number of primary schools, gov't"
la var p_sch_p "Number of primary schools, private"
la var p_sch_near_gp "Nearest primary school status (1 = gov't, 2 = private)"
la var p_sch_near_town "Nearest primary school town"
la var rang_p_sch "Range to nearest primary school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var m_sch_g "Number of middle schools, gov't "
la var m_sch_p "Number of middle schools, private"
la var m_sch_near_gp "Nearest middle school status (1 = gov't, 2 = private)"
la var m_sch_near_town "Nearest middle school town"
la var rang_m_sch "Range to nearest middle school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var s_sch_g "Number of secondary schools, gov't "
la var s_sch_p "Number of secondary schools, private"
la var s_sch_near_gp "Nearest secondary school status (1 = gov't, 2 = private)"
la var s_sch_near_town "Nearest seondary school town"
la var rang_s_sch "Range to nearest secondary school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var s_s_sch_g "Number of senior secondary schools, gov't "
la var s_s_sch_p "Number of senior secondary schools, private"
la var s_s_sch_near_gp "Nearest senior secondary school status (1 = gov't, 2 = private)"
la var s_s_sch_near_town "Nearest senior seondary school town"
la var rang_s_s_sch "Range to nearest senior secondary school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var college_as_g "Number of colleges (arts & sciences), gov't "
la var college_as_p "Number of colleges (arts & sciences), private"
la var college_as_near_gp "Nearest college (arts & sciences) status (1 = gov't, 2 = private)"
la var college_as_near_town "Nearest college (arts & sciences) town"
la var rang_college_as "Range to nearest college (arts & sciences) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var college_en_g "Number of colleges (engineering), gov't "
la var college_en_p "Number of colleges (engineering), private"
la var college_en_near_gp "Nearest college (engineering) status (1 = gov't, 2 = private)"
la var college_en_near_town "Nearest college (engineering) town"
la var rang_college_en "Range to nearest college (engineering) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var college_med_g "Number of colleges (medical), gov't "
la var college_med_p "Number of colleges (medical), private"
la var college_med_near_gp "Nearest college (medical) status (1 = gov't, 2 = private)"
la var college_med_near_town "Nearest college (medical) town"
la var rang_college_med "Range to nearest college (medical) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var college_mng_g "Number of management inst., gov't "
la var college_mng_p "Number of management inst., private"
la var college_mng_near_gp "Nearest management inst. status (1 = gov't, 2 = private)"
la var college_mng_near_town "Nearest management inst. town"
la var rang_college_mng "Range to nearest management inst. (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var college_pol_g "Number of polytechnics, gov't "
la var college_pol_p "Number of polytechnics, private"
la var college_pol_near_gp "Nearest polytechnic status (1 = gov't, 2 = private)"
la var college_pol_near_town "Nearest polytechnic town"
la var rang_college_pol "Range to nearest polytechnics (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var tr_sch_voc_g "Number of training schools (vocational), gov't "
la var tr_sch_voc_p "Number of training schools (vocational), private"
la var tr_sch_voc_near_gp "Nearest training school (vocational) status (1 = gov't, 2 = private)"
la var tr_sch_voc_near_town "Nearest training school (vocational) town"
la var rang_tr_sch_voc "Range to nearest training school (vocational) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var tr_sch_nf_g "Number of training schools (nonformal), gov't "
la var tr_sch_nf_p "Number of training schools (nonformal), private"
la var tr_sch_nf_near_gp "Nearest training school (nonformal) status (1 = gov't, 2 = private)"
la var tr_sch_nf_near_town "Nearest training school (nonformal) town"
la var rang_tr_sch_nf "Range to nearest training school (nonformal) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var dis_sch_g "Number of schools for disabled, gov't "
la var dis_sch_p "Number of schools for disabled, private"
la var dis_sch_near_gp "Nearest school for disabled status (1 = gov't, 2 = private)"
la var dis_sch_near_town "Nearest school for disabled town"
la var rang_dis_sch "Range to nearest school for disabled (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var oth_sch_g "Number of other schools, gov't "
la var oth_sch_p "Number of other schools, private"
la var oth_sch_near_gp "Nearest other school status (1 = gov't, 2 = private)"
la var oth_sch_near_town "Nearest other school town"
la var rang_oth_sch "Range to nearest other school (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ch_cntr "Number of community health centers"
la var ch_cntr_dr_ts "Number of CHC doctors (total strength)"
la var ch_cntr_dr_ip "Number of CHC doctors (in position)"
la var ch_cntr_ps_ts "Number of CHC para staff (total strength)"
la var ch_cntr_ps_ip "Number of CHC para staff (in position)"
la var rang_chc "Range to nearest CHC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ph_cntr "Number of primary health centers"
la var ph_cntr_dr_ts "Number of PHC doctors (total strength)"
la var ph_cntr_dr_ip "Number of PHC doctors (in position)"
la var ph_cntr_ps_ts "Number of PHC para staff (total strength)"
la var ph_cntr_ps_ip "Number of PHC para staff (in position)"
la var rang_phc "Range to nearest PHC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var phs_cnt "Number of primary health sub centers"
la var phs_cnt_dr_ts "Number of PHSC doctors (total strength)"
la var phs_cnt_dr_ip "Number of PHSC doctors (in position)"
la var phs_cnt_ps_ts "Number of PHSC para staff (total strength)"
la var phs_cnt_ps_ip "Number of PHSC para staff (in position)"
la var rang_phsc "Range to nearest PHSC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var mcw_cntr "Number of maternity and child welfare centers"
la var mcw_cntr_dr_ts "Number of MCWC doctors (total strength)"
la var mcw_cntr_dr_ip "Number of MCWC doctors (in position)"
la var mcw_cntr_ps_ts "Number of MCWC para staff (total strength)"
la var mcw_cntr_ps_ip "Number of MCWC para staff (in position)"
la var rang_mcw "Range to nearest MCWC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var tb_cln "Number of T.B. clinics"
la var tb_cln_dr_ts "Number of TBC doctors (total strength)"
la var tb_cln_dr_ip "Number of TBC doctors (in position)"
la var tb_cln_ps_ts "Number of TBC para staff (total strength)"
la var tb_cln_ps_ip "Number of TBC para staff (in position)"
la var rang_tb_cln "Range to nearest TBC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var all_hosp "Number of allopathic hostpitals"
la var all_hosp_dr_ts "Number of AH doctors (total strength)"
la var all_hosp_dr_ip "Number of AH doctors (in position)"
la var all_hosp_ps_ts "Number of AH para staff (total strength)"
la var all_hosp_ps_ip "Number of AH para staff (in position)"
la var rang_all "Range to nearest AH (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var alt_hosp "Number of alternative medicine hostpitals"
la var alt_hosp_dr_ts "Number of AMH doctors (total strength)"
la var alt_hosp_dr_ip "Number of AMH doctors (in position)"
la var alt_hosp_ps_ts "Number of AMH para staff (total strength)"
la var alt_hosp_ps_ip "Number of AMH para staff (in position)"
la var rang_alt "Range to nearest AMH (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var disp "Number of dispensaries"
la var disp_dr_ts "Number of disp doctors (total strength)"
la var disp_dr_ip "Number of disp doctors (in position)"
la var disp_ps_ts "Number of disp para staff (total strength)"
la var disp_ps_ip "Number of disp para staff (in position)"
la var rang_disp "Range to nearest disp (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var vet_hosp "Number of veternary hospitals"
la var vet_hosp_dr_ts "Number of VH doctors (total strength)"
la var vet_hosp_dr_ip "Number of VH doctors (in position)"
la var vet_hosp_ps_ts "Number of VH para staff (total strength)"
la var vet_hosp_ps_ip "Number of VH para staff (in position)"
la var rang_vet "Range to nearest VH (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var mh_clin "Number of mobile health clinic"
la var mh_clin_dr_ts "Number of MHC doctors (total strength)"
la var mh_clin_dr_ip "Number of MHC doctors (in position)"
la var mh_clin_ps_ts "Number of MHC para staff (total strength)"
la var mh_clin_ps_ip "Number of MHC para staff (in position)"
la var rang_mhc "Range to nearest MHC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var fw_cntr "Number of family welfare center"
la var fw_cntr_dr_ts "Number of FWC doctors (total strength)"
la var fw_cntr_dr_ip "Number of FWC doctors (in position)"
la var fw_cntr_ps_ts "Number of FWC para staff (total strength)"
la var fw_cntr_ps_ip "Number of FWC para staff (in position)"
la var rang_fwc "Range to nearest FWC (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ng_med_op "Number of non-gov't med fac (out-patient)"
la var ng_med_iop "Number of non-gov't med fac (in/out-patient)"
la var ng_med_char "Number of non-gov't med fac (charitable)"
la var ng_med_deg_mbbs "Number of non-gov't med fac (practitioner with MBBS degree)"
la var ng_med_deg_oth "Number of non-gov't med fac (practitioner with other degree)"
la var ng_med_deg_none "Number of non-gov't med fac (practitioner with no degree)"
la var ng_med_tp_fh "Number of non-gov't med fac (traditional practitioner and faith healer)"
la var ng_med_ms "Number of non-gov't med fac (medicine shop)"
la var ng_med_oth "Number of non-gov't med fac (others)"
la var tap_tr "Tap water, treated (Y/N)"
la var tap_tr_ay "Tap water, treated, all year (Y/N)"
la var tap_tr_sum "Tap water, treated, summer (Y/N)"
la var tap_untr "Tap water, untreated (Y/N)"
la var tap_untr_ay "Tap water, untreated, all year (Y/N)"
la var tap_untr_sum "Tap water, untreated, summer (Y/N)"
la var well_cov "Well water, covered (Y/N)"
la var well_cov_ay "Well water, covered, all year (Y/N)"
la var well_cov_sum "Well water, covered, summer (Y/N)"
la var well_uncov "Well water, uncovered (Y/N)"
la var well_uncov_ay "Well water, uncovered, all year (Y/N)"
la var well_uncov_sum "Well water, uncovered, summer (Y/N)"
la var handpump "Handpump water (Y/N)"
la var handpump_ay "Handpump water, all year (Y/N)"
la var handpump_sum "Handpump water, summer (Y/N)"
la var tubewell "Tubewell water (Y/N)"
la var tubewell_ay "Tubewell water, all year (Y/N)"
la var tubewell_sum "Tubewell water, summer (Y/N)"
la var spring "Spring water (Y/N)"
la var spring_ay "Spring water, all year (Y/N)"
la var spring_sum "Spring water, summer (Y/N)"
la var river_canal "River/canal water (Y/N)"
la var river_canal_ay "River/canal water, all year (Y/N)"
la var river_canal_sum "River/canal water, summer (Y/N)"
la var tank_lake "Tank/pond/lake water (Y/N)"
la var tank_lake_ay "Tank/pond/lake water, all year (Y/N)"
la var tank_lake_sum "Tank/pond/lake water, summer (Y/N)"
la var other "Other water source (Y/N)"
la var other_ay "Other water source, all year (Y/N)"
la var other_sum "Other water source, summer (Y/N)"
la var drain_cl "Closed drainage (Y/N)"
la var drain_op "Open drainage (Y/N)"
la var drain_none "No drainage (Y/N)"
la var drain_p_cov "Open drainage, pucca covered (Y/N)"
la var drain_p_uncov "Open drainage, pucca uncovered (Y/N)"
la var drain_k "Open drainage, kuccha (Y/N)"
la var drain_disch_wb "Drain charged directly to water bodies (Y/N)"
la var cov_tsc "Covered by Total Sanitation Campaign (Y/N)"
la var com_toilet_bath "Community toilet complex (incl bath) (Y/N)"
la var com_toilet_nobath "Community toilet complex (excl bath) (Y/N)"
la var rpc_sho "Rural production center or sanitary hardware outlet near village (Y/N)"
la var rpm_sho "Rural production mart or sanitary hardware outlet near village (Y/N)"
la var com_waste_disp "Community waste disposal system after house to house collection (Y/N)"
la var com_bg_recycle "Community biogas or recycle waste for production (Y/N)"
la var com_no_waste_disp "No community waste disposal system (Y/N)"
la var post_off_fac "Post office (Y/N)"
la var rang_po "Range to nearest post office (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var post_off_sub_fac "Sub post office (Y/N)"
la var rang_pos "Range to nearest sub post office (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var post_tele_fac "Post and telegraph facilities (Y/N)"
la var rang_post_tele "Range to nearest post and telegraph facilities (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var vill_pin_fac "Village PIN code (Y/N)"
la var rang_vill_pin "Range to nearest village pin code (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var vill_pin "PIN code"
la var phone_ll "Telephone (landlines) (Y/N)"
la var rang_phone_ll "Range to nearest telephone (landlines) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var pco_mobile_fac "Public call office, mobile (Y/N)"
la var rang_pco_mobile "Range to nearest public call office, mobile (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var phone_mob_fac "Mobile phone coverage (Y/N)"
la var rang_phone_mob "Range to nearest mobile phone coverage (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var int_cafe_fac "Internet cafes, common service center (Y/N)"
la var rang_int_cafe "Range to nearest internet cafes, common service center (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var courier_fac "Private courier facility (Y/N)"
la var rang_courier "Range to nearest private courier facility (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var bs_pri_fac "Bus service, public (Y/N)"
la var rang_bs_pri "Range to nearest bus service, public (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var bs_pub_fac "Bus service, private (Y/N)"
la var rang_bs_pub "Range to nearest bus service, private (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var rs_fac "Rail station (Y/N)"
la var rang_rs "Range to nearest rail station (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var auto_fac "Autos or modified autos (Y/N)"
la var rang_auto "Range to nearest autos or modified autos (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var taxi_fac "Taxis (Y/N)"
la var rang_taxi "Range to nearest taxis (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var van_fac "Vans (Y/N)"
la var rang_van "Range to nearest vans (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var tractor_fac "Tractors (Y/N)"
la var rang_tractor "Range to nearest tractors (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var rickshaw_man_fac "Cycle-pulled rickshaws, manual (Y/N)"
la var rang_rickshaw_man "Range to nearest cycle-pulled rickshaws, manual (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var rickshaw_mach_fac "Cycle-pulled rickshaws, machine (Y/N)"
la var rang_rickshaw_mach "Range to nearest cycle-pulled rickshaws, machine (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var carts_fac "Carts driven by animals (Y/N)"
la var rang_carts "Range to nearest carts driven by animals (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ferry_fac "Sea/river/ferry service (Y/N)"
la var rang_ferry "Range to nearest sea/river/ferry service (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ntl_hwy_fac "National highway (Y/N)"
la var rang_ntl_hwy "Range to nearest national highway (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var st_hwy_fac "State highway (Y/N)"
la var rang_st_hwy "Range to nearest state highway (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var road_md_fac "Major district road (Y/N)"
la var rang_road_md "Range to nearest major district road (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var road_od_fac "Other district road (Y/N)"
la var rang_road_od "Range to nearest other district road (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var paved_road_fac "Black-topped (pucca) road (Y/N)"
la var rang_paved_road "Range to nearest black-topped (pucca) road (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var grav_road_fac "Gravel (kuccha) road (Y/N)"
la var rang_grav_road "Range to nearest gravel (kuccha) road (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var wbm_fac "Water bounded macadam (Y/N)"
la var rang_wbm "Range to nearest water bounded macadam (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var aw_road_fac "All weather road (Y/N)"
la var rang_aw_road "Range to nearest all weather road (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var navig_fac "Navigable waterway (river/canal) (Y/N)"
la var rang_navig "Range to nearest navigable waterway (river/canal) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var footpath_fac "Footpath (Y/N)"
la var rang_footpath "Range to nearest footpath (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var atm_fac "ATM (Y/N)"
la var dist_atm_km "Distance to nearest ATM (km)"
la var comm_bank_fac "Commercial bank (Y/N)"
la var rang_comm "Range to nearest commercial bank (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var coop_bank_fac "Cooperative bank (Y/N)"
la var rang_coop "Range to nearest cooperative bank (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var ac_soc_fac "Agricultural credit societies (Y/N)"
la var rang_acs "Range to nearest agricultural credit societies (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var shg_fac "Self-help group (Y/N)"
la var rang_shg "Range to nearest self-help group (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var pub_dist_fac "Public distribution system (Y/N)"
la var rang_pub_dist "Range to nearest public distribution system (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var mandi_fac "Mandis, regular market (Y/N)"
la var rang_mandi "Range to nearest mandis, regular market (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var haat_fac "Weekly haat (Y/N)"
la var rang_haat "Range to nearest weekly haat (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var am_soc_fac "Agricultural marketing society (Y/N)"
la var rang_am_soc "Range to nearest agricultural marketing society (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var nut_cntr_icds_fac "Nutritional centers - ICDS (Y/N)"
la var rang_nut_cntr_icds "Range to nearest nutritional centers - icds (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var nut_cntr_ang "Nutirional centers - Anganwadi center (Y/N)"
la var rang_nut_cntr_ang "Range to nearest nutirional centers - anganwadi center (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var nut_cntr_oth "Nutritional centers - Others (Y/N)"
la var rang_nut_cntr_oth "Range to nearest nutritional centers - others (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var asha_fac "ASHA (accredited social health activist) (Y/N)"
la var rang_asha "Range to nearest asha (accredited social health activist) (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var comm_cntr_fac "Community center, with/without TV (Y/N)"
la var rang_comm_cntr "Range to nearest community center, with/without tv (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var sp_field_fac "Sports field (Y/N)"
la var rang_sp_field "Range to nearest sports field (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var sp_cl_fac "Sports club, rec center (Y/N)"
la var rang_sp_cl "Range to nearest sports club, rec center (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var c_v_hall_fac "Cinema, video hall (Y/N)"
la var rang_cv "Range to nearest cinema, video hall (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var library_fac "Public library (Y/N)"
la var rang_library "Range to nearest public library (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var read_room_fac "Public reading room (Y/N)"
la var rang_read_room "Range to nearest public reading room (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var news_pap_fac "Newspaper (Y/N)"
la var rang_news_pap "Range to nearest newspaper (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var assemb_ps_fac "Assembly polling station (Y/N)"
la var rang_assemb_ps "Range to nearest assembly polling station (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var bdro_fac "Birth/death registration office (Y/N)"
la var rang_bdro "Range to nearest birth/death registration office (1 = <5km, 2 = 5-10km, 3 = >10km)"
la var power_dom "Electricity for domestic use (Y/N)"
la var power_dom_sum_hr "Electricity for domestic use, summer Apr-Sep (hours per day)"
la var power_dom_win_hr "Electricity for domestic use, winter Oct-Mar (hours per day)"
la var power_agr "Electricity for agricultural use (Y/N)"
la var power_agr_sum_hr "Electricity for agricultural use, summer Apr-Sep (hours per day)"
la var power_agr_win_hr "Electricity for agricultural use, winter Oct-Mar (hours per day)"
la var power_com "Electricity for commercial use (Y/N)"
la var power_com_sum_hr "Electricity for commercial use, summer Apr-Sep (hours per day)"
la var power_com_win_hr "Electricity for commercial use, winter Oct-Mar (hours per day)"
la var power_all "Electricity for all uses (Y/N)"
la var power_all_sum_hr "Electricity for all uses, summer Apr-Sep (hours per day)"
la var power_all_win_hr "Electricity for all uses, winter Oct-Mar (hours per day)"
la var agr_comm1 "Agricultural commodity 1"
la var man_comm1 "Manufacturers commodity 1"
la var hand_comm1 "Handicrafts commodity 1"
la var agr_comm2 "Agricultural commodity 2"
la var man_comm2 "Manufacturers commodity 2"
la var hand_comm2 "Handicrafts commodity 2"
la var agr_comm3 "Agricultural commodity 3"
la var man_comm3 "Manufacturers commodity 3"
la var hand_comm3 "Handicrafts commodity 3"
la var area_fores "Area of forest (hectares)"
la var area_non_ag "Non-agricultural land area (hectares)"
la var area_na_cu "Barren/un-cultivable land area (hectares)"
la var area_pasture "Pasture/grazing land area (hectares)"
la var area_treecrop "Misc tree crop land area (hectares)"
la var area_cult_waste "Area of culturable waste (hectares)"
la var area_fallow_oth "Other fallows land area (hectares)"
la var area_fallow_cur "Current fallows land area (hectares)"
la var area_sown "Net area sown (hectares)"
la var area_unirrigated "Total unirrigated land area (hectares)"
la var area_irrigated "Area irrigated (hectares)"
la var area_irr_canal "Area irrigated by canals (hectares)"
la var area_irr_w_twell "Area irrigated by wells/tube wells (hectares)"
la var area_irr_tank_lake "Area irrigated by tanks/lakes (hectares)"
la var area_irr_waterfall "Area irrigated by waterfall (hectares)"
la var area_irr_oth "Area irrigated by other source (hectares)"
la var near_town "Nearest town"
la var dist_town "Distance to nearest town (km)"
}


*Tag variables as 2011  
foreach v of varlist ref_year-dist_town {
  rename `v' `v'_11
}
}


* Save as 2011 VD dataset
{
drop t_hh_11 t_m_11-st_f_11 
rename t_p_vd_11 t_p_vd11
drop if t_p_vd11==0
drop if inlist(st_code11,4,7,25,26,30,31,34,35)  // drop non-RGGVY states 
duplicates drop
sort st_code11 dt_code11 vi_code11
compress
save "$vd11/vd_2011.dta", replace

keep st_code11 dt_code11 bk_code* vi_code11 village_vd11 t_p_vd11 vd11_id
save "$vd11/vd_2011_names.dta", replace

}

********************************************************************************
********************************************************************************
